export function utils(vm) {
	return {
		isDebug: false,
		formatDate(d, format) {
			if (!d) {
				return "";
			}
			if (!format) {
				format = "YYYY-MM-DD HH:mm:ss";
			}

			let result = vm.$moment(d).format(format);

			return result
		},
		formatDateLocal(d, format) {
			if (!d) {
				return "";
			}
			let locale = vm.$t("message").msg6
			if (!format) {
				format = "YYYY-MM-DD HH:mm:ss";
			}
			if (locale) {
				vm.$moment.locale(locale.substring(0, 2))
			}
			let result = vm.$moment(d).format(format);

			if (locale) {
				vm.$moment.locale("en")
			}
			return result
		},
		formatDateNow() {
			return this.formatDate(new Date())
		},
		getDataInfo() {
			return uni.getStorageSync("bluetooth.dataInfo")
		},
		getDataInfo$Weight() {
			return uni.getStorageSync("bluetooth.$WEIGHT")
		},
		screenHeight() {
			let res = uni.getSystemInfoSync()
			return res.screenHeight * (750 / res.windowWidth) + "rpx"
		},
		yesterday() {
			let day1 = new Date();
			day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
			return this.formatDate(day1, "YYYY-MM-DD")
		},
		copyToClipboard(txt) {
			let oInput = document.createElement('textarea');
			oInput.value = txt;
			document.body.appendChild(oInput);
			oInput.select(); // 选择对象
			document.execCommand("Copy"); // 执行浏览器复制命令
			document.body.removeChild(oInput);
			oInput.style.display = 'none';

			vm.$message({
				message: '复制完成~！',
				type: 'success'
			});
		},
		uploadFileLocal(file, callBack) {
			if (!file || !callBack) {
				return
			}
			const param = new FormData();
			param.append('file', file.file)

			vm.$http.post('/apply/file/upload/image', param, {
					headers: {
						'Content-Type': 'application/octet-stream',
					},
				})
				.then(res => {
					if (!res.data.success) {
						vm.$message({
							showClose: true,
							message: res.data.msg,
							type: 'error'
						});
						return
					}
					callBack(this.imageDo(res.data.data));
				}).catch(err => {
					console.log(err);
				});
		},
		uploadFile(file, callBack) {
			if (!file || !callBack) {
				return
			}
			vm.$http.post('/apply/manage/ossFile/image/oss_upload', {
					"fileName": file.file.name
				})
				.then(res => {
					if (!res.data.success) {
						vm.$message({
							showClose: true,
							message: res.data.msg,
							type: 'error'
						});
						return
					}
					let url = res.data.data;
					vm.$http.put(url, file.file, {
						headers: {
							'Content-Type': 'application/octet-stream',
							'x-oss-meta-author': 'aliy',
						},
					}).then(res => {
						let pos = url.indexOf("?");
						if (pos !== -1) {
							vm.$nextTick(() => {
								callBack(url.substring(0, pos));
							});
						}
					}).catch(res => {
						vm.$message({
							showClose: true,
							message: res.data.msg,
							type: 'error'
						});

					})
				}).catch(err => {
					console.log(err);
				});
		},

	}

}
